package com.tunnel.common.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 编码器，适用于服务端和客户端之间的通讯编码
 */
public class EncoderHandler extends MessageToByteEncoder<Protocol> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Protocol msg, ByteBuf out) throws Exception {
        out.writeInt(msg.getCommonType());
        out.writeLong(msg.getSequenceId());
        out.writeInt(msg.getLength());
        out.writeBytes(msg.getBody().getBytes());
    }
}
